package com.vates.vsp.gui.reserva;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import com.vates.vsp.Principal;
import com.vates.vsp.entidades.Reserva;
import com.vates.vsp.exceptions.ReservaNoExistException;
import com.vates.vsp.exceptions.ValidationException;
import com.vates.vsp.gui.models.ReservaModel;
import com.vates.vsp.servicios.reservas.ReservaService;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ListSelectionModel;

public class VentanaMostrarTodasReservas extends JDialog {
	private static final long serialVersionUID = 1L;
	
	private ReservaService reservaSrv;
	
	private final JPanel contentPanel = new JPanel();
	private JTable table;

	public VentanaMostrarTodasReservas() {

		initComponents();
	}

	private void initComponents() {
		reservaSrv = (ReservaService) Principal.context.getBean("reservaSrv");
		setBounds(100, 100, 769, 436);
		setModal(true);
		setResizable(true);
		setTitle("- Listado de Reservas -");
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(Color.WHITE);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setLocationRelativeTo(null);
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBorder(new TitledBorder(null, "Todas las reservas", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JScrollPane scrollPane = new JScrollPane();
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane.setViewportView(table);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(4)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
					.addGap(4))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(8)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)
					.addGap(4))
		);
		panel.setLayout(gl_panel);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(5)
					.addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(6)
					.addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(Color.WHITE);
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton btnNuevaReserva = new JButton("Nueva...");
				btnNuevaReserva.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						nueva();
					}
				});
				btnNuevaReserva.setActionCommand("OK");
				buttonPane.add(btnNuevaReserva);
				getRootPane().setDefaultButton(btnNuevaReserva);
			}
			{
				JButton btnCerrar = new JButton("Cerrar");
				btnCerrar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				
				JButton btnEliminarSeleccin = new JButton("Eliminar selecci\u00F3n");
				btnEliminarSeleccin.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						eliminarSeleccion();
					}
				});
				
				JButton btnBuscar = new JButton("Buscar...");
				btnBuscar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						buscar();
					}
				});
				buttonPane.add(btnBuscar);
				
				JButton btnEditarSeleccion = new JButton("Editar selecci\u00F3n...");
				btnEditarSeleccion.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						editarSeleccion();
					}
				});
				buttonPane.add(btnEditarSeleccion);
				buttonPane.add(btnEliminarSeleccin);
				btnCerrar.setActionCommand("Cancel");
				buttonPane.add(btnCerrar);
			}
		}
		
		actualizarTabla();
	}

	private void actualizarTabla() {
		table.setModel(new ReservaModel(reservaSrv.buscarTodasReservas()));
	}
	
	private void nueva() {
		JDialog jd = new VentanaReservaNueva();
		jd.setVisible(true);
		actualizarTabla();
	}
	
	private Reserva obtenerReservaSeleccionada() {
		try {
			Long id = (Long) table.getValueAt(table.getSelectedRow(), 0);
			return reservaSrv.buscarReservaPorId(id);
		} catch (ReservaNoExistException e) { }
		return null;
	}
	
	private void eliminarSeleccion() {
		try {
			if(table.getSelectedRowCount() != 0) {
				Reserva reserva = obtenerReservaSeleccionada();
				if(JOptionPane.showConfirmDialog(this, "Desea eliminar la reserva " + reserva.getIdReserva() + "?", "Eliminar Reserva", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0) {
						reservaSrv.bajaReserva(reserva);
						actualizarTabla();
				}
			}
		} catch (ReservaNoExistException e) {
	
		} catch (ValidationException e) {

		}
	}
	
	private void editarSeleccion() {
		if(table.getSelectedRowCount() > 0) {
			JDialog jd = new VentanaEditarReserva(obtenerReservaSeleccionada());
			jd.setVisible(true);
			actualizarTabla();
		}
	}
	
	private void buscar() {
		JDialog jd = new VentanaEditarReserva();
		jd.setVisible(true);
		actualizarTabla();
	}
	
}
